package com.anyou.framework.aspectj;

import com.anyou.common.annotation.DataScope;
import com.anyou.common.annotation.ShopScope;
import com.anyou.common.core.domain.BaseEntity;
import com.anyou.common.core.domain.entity.SysRole;
import com.anyou.common.core.domain.entity.SysUser;
import com.anyou.common.core.domain.model.LoginUser;
import com.anyou.common.utils.SecurityUtils;
import com.anyou.common.utils.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * 商家数据过滤处理
 *
 * @author ruoyi
 */
@Aspect
@Component
public class ShopScopeAspect
{
    /**
     * 全部数据权限
     */
    public static final String DATA_SCOPE_ALL = "1";

    /**
     * 自定数据权限
     */
    public static final String DATA_SCOPE_CUSTOM = "2";

    /**
     * 部门数据权限
     */
    public static final String DATA_SCOPE_DEPT = "3";

    /**
     * 部门及以下数据权限
     */
    public static final String DATA_SCOPE_DEPT_AND_CHILD = "4";

    /**
     * 仅本人数据权限
     */
    public static final String DATA_SCOPE_SELF = "5";

    /**
     * 数据权限过滤关键字
     */
    public static final String DATA_SCOPE = "shopScope";

    @Before("@annotation(controllerDataScope)")
    public void doBefore(JoinPoint point, ShopScope controllerDataScope) throws Throwable
    {
        clearDataScope(point);
        handleDataScope(point, controllerDataScope);
    }

    protected void handleDataScope(final JoinPoint joinPoint, ShopScope controllerDataScope)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            SysUser currentUser = loginUser.getUser();
            boolean shopUser = currentUser.isShopUser();
            boolean admin = currentUser.isAdmin();
            if (StringUtils.isNotNull(currentUser) && !admin&&shopUser)
            {
                //如果当前用户的角色不是超级管理员,并角色是商家用户时,才执行过滤数据的方法
                dataScopeFilter(joinPoint, currentUser, controllerDataScope.baseTableAlias());
            }
        }
    }

    /**
     * 商家数据范围过滤
     *
     * @param joinPoint 切点
     * @param user 用户
     * @param baseTableAlias 别名
     */
    public static void dataScopeFilter(JoinPoint joinPoint, SysUser user, String baseTableAlias)
    {
        StringBuilder sqlString = new StringBuilder();


        sqlString.append(StringUtils.format(
                " OR {}.car_parts_shop_id IN ( SELECT u.car_parts_shop_id FROM sys_user u WHERE u.user_id = {} ) ", baseTableAlias,
                user.getUserId()));

        if (StringUtils.isNotBlank(sqlString.toString()))
        {
            Object params = joinPoint.getArgs()[0];
            if (StringUtils.isNotNull(params)&& params instanceof BaseEntity )
            {
                BaseEntity baseEntity = (BaseEntity) params;
                String sql=" AND (" + sqlString.substring(4) + ")";
                baseEntity.getParams().put(DATA_SCOPE, sql);
            }
        }
    }

    /**
     * 拼接权限sql前先清空params.dataScope参数防止注入
     */
    private void clearDataScope(final JoinPoint joinPoint)
    {
        Object params = joinPoint.getArgs()[0];
        if (StringUtils.isNotNull(params) && params instanceof BaseEntity)
        {
            BaseEntity baseEntity = (BaseEntity) params;
            baseEntity.getParams().put(DATA_SCOPE, "");
        }
    }
}
